pub fn luhn(cc_number: &str) -> bool {
    let card_num = cc_number.replace(" ", "");
    if card_num.is_empty() || card_num.len() < 2 { return false; }
    let li = card_num.chars().rev().enumerate();
    let mut sum = 0_u32;
    for (index, c) in li {
        if c < '0' || c > '9' {
            return false;
        } else {
            let result: u32 = c.to_digit(10).unwrap();
            let idx = index + 1;
            if idx % 2 == 0 {
                let i = result * 2;
                if i >= 10 {
                    let i1 = i % 10;
                    let i2 = i / 10;
                    sum = sum + i1 + i2;
                } else {
                    sum = sum + i;
                }
            } else {
                sum = sum + result;
            }
        }
    }
    return !card_num.is_empty() && sum % 10 == 0;
}

#[test]
fn test_non_digit_cc_number() {
    assert!(!luhn("foo"));
    assert!(!luhn("foo 0 0"));
}

#[test]
fn test_empty_cc_number() {
    assert!(!luhn(""));
    assert!(!luhn(" "));
    assert!(!luhn("  "));
    assert!(!luhn("    "));
}

#[test]
fn test_single_digit_cc_number() {
    assert!(!luhn("0"));
}

#[test]
fn test_two_digit_cc_number() {
    assert!(luhn(" 0 0 "));
}

#[test]
fn test_valid_cc_number() {
    assert!(luhn("4263 9826 4026 9299"));
    assert!(luhn("4539 3195 0343 6467"));
    assert!(luhn("7992 7398 713"));
}

#[test]
fn test_invalid_cc_number() {
    assert!(!luhn("4223 9826 4026 9299"));
    assert!(!luhn("4539 3195 0343 6476"));
    assert!(!luhn("8273 1232 7352 0569"));
}

#[allow(dead_code)]
fn main() {}